package com.qianniu.napi.admin.web.goods.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.Goodsfav;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.admin.service.IGoodsfavService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-04
 */
@RestController
public class GoodsfavController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsfavController.class);

    @Autowired
    private IGoodsfavService goodsfavService;
    @Autowired
    private IGoodsService goodsService;

    @RequestMapping("/api/goodsfav/list")
    @ResponseBody
    public Jinfo list(HttpServletRequest req,
                      @RequestParam(value = "type", required = false) Integer type,
                      @RequestParam(value = "pageIndex", required = false) int pageIndex,
                      @RequestParam(value = "pageSize", required = false) int pageSize) {
        Jinfo json = new Jinfo();
        if(type==null)type = 1;
        Page page = new Page(pageIndex, pageSize);
        page = goodsfavService.selectPage(page, new EntityWrapper<Goodsfav>()
                .eq("uid", ObjectUtil.getUid())
                .eq("type", type)
                .eq("state", 1));
        json.setInfo(page.getRecords());
        return json;
    }

    @RequestMapping("/api/goodsfav/add")
    @ResponseBody
    public Jinfo addFavor(@RequestParam(value = "gid", required = false) Long gid,
                          @RequestParam(value = "price", required = false) BigDecimal price,
                          @RequestParam(value = "pic", required = false) String pic,
                          @RequestParam(value = "name", required = false) String name,
                          @RequestParam(value = "type", required = false) Integer type,
                          HttpServletRequest req) {
        Jinfo json = new Jinfo();
        Goodsfav gf = new Goodsfav();
        type = type == null ? 1 : type;
        if(type==1){
            Goods g =goodsService.selectOne(new EntityWrapper<Goods>().setSqlSelect("id,type").eq("id",gid));
            gf.setYxtype(g==null?0:g.getType());
        }
        gf.setGid(gid);
        gf.setUid(ObjectUtil.getUid());
        gf.setState(1);
        gf.setPic(pic);
        gf.setPrice(price);
        gf.setType(type);
        gf.setGoodsName(name);
        gf.setMid(ObjectUtil.getMid());
        gf.insert();

        return json;
    }

    @RequestMapping("/api/goodsfav/del")
    @ResponseBody
    public Jinfo delFavor(@RequestParam(value = "gid", required = false) Long gid,
                          @RequestParam(value = "type", required = false) Integer type) {
        Jinfo json = new Jinfo();
        Goodsfav gf = new Goodsfav();
        if(type==null)type=1;
        gf.setGid(gid);
        gf.setUid(ObjectUtil.getUid());
        gf.setType(type);
        gf.setState(0);
        goodsfavService.update(gf, new EntityWrapper<Goodsfav>()
                .eq("gid", gf.getGid())
                .eq("type", type)
                .eq("uid", gf.getUid()));

        return json;
    }

    @RequestMapping("/api/goodsfav/modify")
    @ResponseBody
    public Jinfo goodsfavUpdate(@Valid Goodsfav goodsfav, HttpServletRequest req) {
        Jinfo json = new Jinfo();
        goodsfav.setModifytime(new Date());
        goodsfav.updateById();
        return json;
    }

    @RequestMapping("/api/goodsfav/detail")
    @ResponseBody
    public Jinfo getGoodsfavDetailById(@RequestParam("gid") long gid, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        Goodsfav goodsfav = goodsfavService.selectOne(new EntityWrapper<Goodsfav>().eq("gid", gid)
                .eq("uid", ObjectUtil.getUid()));
        info.setInfo(goodsfav);
        return info;
    }

}
